package top.noteday.interceptor;

import entity.result.BaseResult;
import enums.http.ResponseCodeEnum;
import exception.MyApplicationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class WebExceptionHandle {

	/**
	 * 拦截自定义异常信息
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(MyApplicationException.class)
	@ResponseBody
	public BaseResult handleMyApplicatioException(MyApplicationException e) {
		e.printStackTrace();
		BaseResult result = new BaseResult();
		result.setCode(e.getCode());
		result.setMess(e.getMessage());
		return result;
	}

	/**
	 * 拦截系统异常信息
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public BaseResult handleRuntimeException(RuntimeException e) {
		e.printStackTrace();
		return new BaseResult().responseFail(ResponseCodeEnum.SYSTEM_ERROR);
	}
	
	/**
	 * json转换异常
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(HttpMessageNotReadableException.class)
	@ResponseBody
	public BaseResult handleRuntimeException(HttpMessageNotReadableException e) {
		e.printStackTrace();
		return new BaseResult().responseFail(ResponseCodeEnum.JSON_PARAM_ERROR);
	}
}